package com.chtw;

import com.chtw.utils.aec.AesException;
import com.chtw.utils.aec.SHA1;
import org.springframework.beans.factory.annotation.Value;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * @author chtw
 * @date 2020-06-19 07:35
 */
public class Test {

    public static void main(String[] args) throws AesException {
        String s = getSign("gHKLFDFhahdkllsfjjshfkk", "1592650790", "1663953783");
        System.out.println(s);

        s = SHA1.getSign("gHKLFDFhahdkllsfjjshfkk", "1592650790", "1663953783");
        System.out.println(s);

    }

    public static String getSign(String token, String timestamp, String nonce) {
        try {
            String[] array = new String[] { token, timestamp, nonce };
            StringBuffer sb = new StringBuffer();
            // 字符串排序
            Arrays.sort(array);
            for (int i = 0; i < 3; i++) {
                sb.append(array[i]);
            }
            String str = sb.toString();
            // SHA1签名生成
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes());
            byte[] digest = md.digest();

            StringBuffer hexstr = new StringBuffer();
            String shaHex = "";
            for (int i = 0; i < digest.length; i++) {
                shaHex = Integer.toHexString(digest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexstr.append(0);
                }
                hexstr.append(shaHex);
            }
            return hexstr.toString();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }
}
